﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using My.Data.Factories;
using My.Data.DataAccess;
using My.Data.BusinessObject;

namespace NHibernate.App
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }
        private IAuthenticationDao authenticationDao = DataAccessFactory.LoadAuthenticationDao();

        private void RegisterBtn_Click(object sender, EventArgs e)
        {


            IList<Authentication> a = authenticationDao.GetAll();
            Authentication auth = authenticationDao.GetById(1);
            if (auth.SuperUser != AuthenticationBox.Text && auth.Teacher != AuthenticationBox.Text && auth.Student != AuthenticationBox.Text)
            {
                MessageBox.Show("zita kodiko apo ton kirio petalidi");
            }
            else
            {
                ITransaction transaction = null;
                ISession session = ISessionManager.Instance.CreateISession();
                transaction = session.BeginTransaction();

                Person newPerson = new Person();
                newPerson.Name = NameBox.Text;
                newPerson.SureName = SureNameBox.Text;
                newPerson.AM = int.Parse(AMBox.Text);
                newPerson.Address = AddressBox.Text;
                newPerson.Phone = long.Parse(PhoneBox.Text);

                Login newLogin = new Login();
                newLogin.UserName = UserNameBox.Text;
                newLogin.PassWord = PassWordBox.Text;
                if (auth.SuperUser == AuthenticationBox.Text)
                {newLogin.Type = 1;
                }
                else if (auth.Teacher == AuthenticationBox.Text)
                {
                    newLogin.Type = 2;
                }
                else 
                {
                    newLogin.Type = 3;
                }
                newLogin.person = newPerson;

                session.Save(newLogin);
                transaction.Commit();
                session.Close();
            }
        }
    }
}